package com.tang.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fasterxml.jackson.databind.ser.Serializers;
import com.tang.reggie.common.BaseContext;
import com.tang.reggie.exception.CustomException;
import com.tang.reggie.mapper.ShoppingCartMapper;
import com.tang.reggie.pojo.ShoppingCart;
import com.tang.reggie.service.ShoppingCartService;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ShoppingCartServiceImpl extends ServiceImpl<ShoppingCartMapper, ShoppingCart> implements ShoppingCartService {



    @Override
    public ShoppingCart add(ShoppingCart shoppingCart) {
        //获取用户id
        Long userId = BaseContext.getCurrentId();
        //添加到购物车中
        shoppingCart.setUserId(userId);

        //判断添加的购物车条目是否已经在数据库中存在，已存在那么就数量加一即可
        LambdaQueryWrapper<ShoppingCart> shoppingCartLambdaQueryWrapper = new LambdaQueryWrapper<>();
        shoppingCartLambdaQueryWrapper.eq(ShoppingCart::getUserId,userId);
        shoppingCartLambdaQueryWrapper.eq(shoppingCart.getDishId()!=null,ShoppingCart::getDishId,shoppingCart.getDishId());
        shoppingCartLambdaQueryWrapper.eq(shoppingCart.getSetmealId()!=null,ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        //select * from shopping_cart where user_id=? and dish_id/setmeal_id=?
        ShoppingCart shoppingCartOne = super.getOne(shoppingCartLambdaQueryWrapper);
        if (shoppingCartOne!=null){
            //说明在数据库中已存在,数量加一
            Integer number = shoppingCartOne.getNumber();
            shoppingCartOne.setNumber(number+1);
            super.updateById(shoppingCartOne);
        }else {//不存在，添加数据到购物车中
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            super.save(shoppingCart);
            shoppingCartOne=shoppingCart;
        }
        return shoppingCartOne;
    }

    /**
     * 购物车列表查询
     * @param userId
     * @return
     */
    @Override
    public List<ShoppingCart> getAll(Long userId) {
        LambdaQueryWrapper<ShoppingCart> shoppingCartLambdaQueryWrapper = new LambdaQueryWrapper<>();
        shoppingCartLambdaQueryWrapper.eq(ShoppingCart::getUserId,userId);
        shoppingCartLambdaQueryWrapper.orderByDesc(ShoppingCart::getCreateTime);
        List<ShoppingCart> shoppingCartList = super.list(shoppingCartLambdaQueryWrapper);
        return shoppingCartList;
    }

    /**
     * 购物车清空
     * @param currentId
     */
    @Override
    public void clean(Long currentId) {
        LambdaQueryWrapper<ShoppingCart> shoppingCartLambdaQueryWrapper = new LambdaQueryWrapper<>();
        shoppingCartLambdaQueryWrapper.eq(currentId!=null,ShoppingCart::getUserId,currentId);
        super.remove(shoppingCartLambdaQueryWrapper);
    }

    /**
     * 减少数量
     * @param shoppingCart
     * @return
     */
    @Override
    public ShoppingCart sub(ShoppingCart shoppingCart) {

        Long currentId = BaseContext.getCurrentId();//获取用户id

        LambdaQueryWrapper<ShoppingCart> shoppingCartLambdaQueryWrapper = new LambdaQueryWrapper<>();
        shoppingCartLambdaQueryWrapper.eq(shoppingCart.getSetmealId()!=null,ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        shoppingCartLambdaQueryWrapper.eq(shoppingCart.getDishId()!=null,ShoppingCart::getDishId,shoppingCart.getDishId());
        shoppingCartLambdaQueryWrapper.eq(ShoppingCart::getUserId,currentId);
        ShoppingCart one = super.getOne(shoppingCartLambdaQueryWrapper);
        if (one==null){
            throw new CustomException("购物车中已不存在该套餐了，不能减数量");
        }
        Integer number = one.getNumber();
        if (number==1){
            super.removeById(one);
            one.setNumber(0);
            return one;
        }
        one.setNumber(number-1);
        //如果减少到0了，那么就直接清掉这条数据
        super.updateById(one);
        return one;
    }
}
